<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="ID" prop="userInfoId">
        <el-input
          v-model="queryParams.userInfoId"
          placeholder="请输入用户ID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="所属银行" prop="perfectionBelong">
        <el-select
          v-model="queryParams.perfectionBelong"
          placeholder="分行/支行"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <el-table v-loading="loading" :data="perfectionList" @selection-change="handleSelectionChange" style="width: 100%;">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" prop="perfectionId" width="75" align="center"/>
      <el-table-column label="用户ID" prop="userInfoId" width="75" align="center"/>
      <el-table-column label="用户姓名" prop="userName" :show-overflow-tooltip="true" width="120" align="center"/>
      <!-- <el-table-column label="所属银行" prop="userBelong" :show-overflow-tooltip="true" width="120" align="center"/> -->
      <el-table-column label="KYC完善度" prop="full" :show-overflow-tooltip="true" fixed-width align="center">
        <el-table-column label="个人及家庭情况" prop="userinfo" :show-overflow-tooltip="true" align="center"/>
        <el-table-column label="工作及学习情况" prop="workinfo" :show-overflow-tooltip="true" align="center"/>
        <el-table-column label="金融产品覆盖情况" prop="productinfo" :show-overflow-tooltip="true" align="center"/>
        <el-table-column label="客户MCM潜力洞察" prop="potentialinfo" :show-overflow-tooltip="true" align="center"/>
        <el-table-column label="总体" prop="perfection" :show-overflow-tooltip="true" align="center"/>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>=0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  <div>
    <!-- 柱状图 -->
    <div align="center" style="margin-top: 50px;" fixed-width>
      <div class="echart" id="mychart" :style="myChartStyle"></div>
    </div>
    <div align="center" style="margin-top: 50px;" fixed-width>
      <div class="echart" id="mychart2" :style="myChartStyle"></div>
    </div>

    <!-- 柱状图显示在同一行 -->
    <!-- <div style="margin-top: 50px;width: 50%; float: left; overflow: hidden" fixed-width>
      <div class="echart" id="mychart" :style="myChartStyle"></div>
    </div>
    <div style="margin-top: 50px;width: 50%; float: left; overflow: hidden" fixed-width>
      <div class="echart" id="mychart2" :style="myChartStyle"></div>
    </div> -->

  </div>

  </div>
</template>

<script>
import { listPerfection, getPerfection, delPerfection, addPerfection, updatePerfection } from "@/api/kycinfo/perfection";


import * as echarts from "echarts";
export default {
  name: "Perfection",
  data() {
    return {
      xData: ["个人及家庭情况", "工作及学习情况", "金融产品覆盖情况", "客户MCM潜力洞察", "总体"], //横坐标
      yData: [26.5, 26.5, 26.5, 7.5, 87], //数据
      // yData: [this.calculateAverage('userinfo'), this.calculateAverage('workinfo'), this.calculateAverage('productinfo'), this.calculateAverage('potentialinfo'),this.calculateAverage('perfection')],
      myChartStyle: { float: "left", width: "100%", height: "400px" }, //图表样式

      // options: [{
      //     value: '选项1',
      //     label: '分行'
      //   }, {
      //     value: '选项2',
      //     label: '支行'
      //   },
      // ],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // kyc完善度表格数据
      perfectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        perfectionId: null,
        userInfoId: null,
        userName:null,
        userinfo:null,
        workinfo:null,
        productinfo:null,
        potentialinfo:null,
        perfection:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "kyc用户id不能为空", trigger: "blur" }
        ],
      }
    };
  },

  mounted() {
    this.initEcharts();
    // this.calculateAverage();
    this.initEcharts2();
  },

  created() {
    this.getList();   
  },

  methods: {
    /** 查询完整度列表 */
    getList() {
      this.loading = true;
      listPerfection(this.queryParams).then(response => {
          this.perfectionList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.log(this.perfectionList); 
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    
    // 表单重置
    reset() {
      // if (this.$refs.menu != undefined) {
      //   this.$refs.menu.setCheckedKeys([]);
      // }
      // this.menuExpand = false,
      // this.menuNodeAll = false,
      // this.deptExpand = true,
      // this.deptNodeAll = false,
      this.form = {
        perfectionId: undefined,
        userInfoId:undefined,
        userName: undefined,
        // userBelong: "0",
        userinfo: undefined,
        workinfo: undefined,
        productinfo: undefined,
        potentialinfo: undefined,
        perfection: undefined,

        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
        // menuIds: [],
        // deptIds: [],
        // menuCheckStrictly: true,
        // deptCheckStrictly: true,
        // remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.perfectionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    calculateAverage(property) {
      // 将字符串转化为数值型并计算平均值
      const sum = this.perfectionList.reduce((total, user) => {
        const value = parseFloat(user[property].replace('%', '')); // 去掉百分号并转化为浮点数
        return total + value;
      }, 0);
      return sum / total;
    },

    initEcharts() {
      // 基本柱状图
      const option = {
        title: {
          text: '分行KYC完善度评估',
          left:'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}%'
        },
        legend: {
          orient: 'vertical',
          x: 'right',
          y: 'top',
          data: ['评估概率']
        },
        xAxis: {
          data: this.xData,
          type: 'category',
          axisLabel:{
            interval: 0
          },
        },
        yAxis: {
        },
        series: [
          {
            type: "bar", //形状为柱状图
            data: this.yData,
            // data: [this.calculateAverage('userinfo'), this.calculateAverage('workinfo'), this.calculateAverage('productinfo'), this.calculateAverage('potentialinfo'),this.calculateAverage('perfection')],
            name: '评估概率',
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              },
              color: function (params) {
                // 自定义颜色
                var colorList = ['#A1A9D0', '#F0988C','#B883D4','#9E9E9E','#CFEAF1']
                return colorList[params.dataIndex]
              }
            },
            label: {
                    show: true, //开启显示
                    position: 'top', //在上方显示
                    formatter: '{c}%',//显示百分号
                    textStyle: { //数值样式
                        color: 'gray',//字体颜色
                        fontSize: 10//字体大小
                    }
            }
          }
        ]
      };
      const myChart = echarts.init(document.getElementById("mychart"));
      myChart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },

    

    initEcharts2() {
      // 基本柱状图
      const option = {
        title: {
          text: '支行KYC完善度评估',
          left:'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}%'
        },
        legend: {
          orient: 'vertical',
          x: 'right',
          y: 'top',
          data: ['评估概率']
        },
        xAxis: {
          data: this.xData,
          type: 'category',
          axisLabel:{
            interval: 0
          },
        },
        yAxis: {},
        series: [
          {
            type: "bar", //形状为柱状图
            data: this.yData,

            name: '评估概率',
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              },
              color: function (params) {
                // 自定义颜色
                var colorList = ['#A1A9D0', '#F0988C','#B883D4','#9E9E9E','#CFEAF1']
                return colorList[params.dataIndex]
              }
            },
            label: {
                    show: true, //开启显示
                    position: 'top', //在上方显示
                    formatter: '{c}%',//显示百分号
                    textStyle: { //数值样式
                        color: 'gray',//字体颜色
                        fontSize: 10//字体大小
                    }
            }
          }
        ]
      };
      const myChart2 = echarts.init(document.getElementById("mychart2"));
      myChart2.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart2.resize();
      });
    },

  }

};
</script>
